package com.jinxin.platform.cddjnhjcxtsysset.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeExpend2;
import com.jinxin.platform.cddjnhjcxtsysset.mapper.CddjnhjcxtsyssetHomeExpend2Mapper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
public class ExpendExcelListener extends AnalysisEventListener<ExpendExcel> {
    private CddjnhjcxtsyssetHomeExpend2Mapper homeExpend2Mapper;
    public int insertedCount;
    public int updatedCount;
    public String msg;

    public ExpendExcelListener(CddjnhjcxtsyssetHomeExpend2Mapper homeExpend2Mapper) {
        this.homeExpend2Mapper = homeExpend2Mapper;
    }
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 1000;
    List<ExpendExcel> list = new ArrayList<>();


    @Override
    public void invoke(ExpendExcel studentExcel, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", JSON.toJSONString(studentExcel));
        list.add(studentExcel);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        for(ExpendExcel v: list){
            Integer jsyear = v.getJsyear();
            Integer jsmonth = v.getJsmonth();
            String orgId = v.getOrgId();
            if(jsyear == null || jsmonth == null || StringUtil.isEmpty(orgId)){
                log.error("结算年，结算月和组织ID均不能为空");
                continue;
            }
            HashMap param = new HashMap();
            param.put("jsyear",jsyear);
            param.put("jsmonth",jsmonth);
            param.put("orgId",orgId);
            CddjnhjcxtsyssetHomeExpend2 expend = homeExpend2Mapper.selectOne(param);
            if(expend == null){
                expend = new CddjnhjcxtsyssetHomeExpend2();
                expend.setId(Utils.getUuid());
                expend.setJsyear(jsyear);
                expend.setJsmonth(jsmonth);
                expend.setDfzc(v.getDfzc());
                expend.setSfzc(v.getSfzc());
                expend.setRlfzc(v.getRlfzc());
                expend.setTrqzc(v.getTrqzc());
                expend.setQyzc(v.getQyzc());
                expend.setMyzc(v.getMyzc());
                expend.setYhqzc(v.getYhqzc());
                expend.setOrgId(v.getOrgId());
                insertedCount += homeExpend2Mapper.insert(expend);
            }else {
                expend.setDfzc(v.getDfzc());
                expend.setSfzc(v.getSfzc());
                expend.setRlfzc(v.getRlfzc());
                expend.setTrqzc(v.getTrqzc());
                expend.setQyzc(v.getQyzc());
                expend.setMyzc(v.getMyzc());
                expend.setYhqzc(v.getYhqzc());
                updatedCount += homeExpend2Mapper.updateByKey(expend);
            }
        }
        msg = "成功导入"+ insertedCount +"条数据,更新" + updatedCount + "条数据.";
        log.info("存储或更新数据库成功！");
    }
}
